<?php

namespace app\api\model\sign;

use app\api\model\Setting;
use app\api\model\user\PointsLog;

class Sign extends \app\common\model\sign\Sign
{
    public $point = 0;

    function sign($user_id)
    {
        //$user_id = $this->user['user_id'];
        $setting = Setting::getItem('sign');

        if (!$setting['is_open']) {
            $this->error = '签到功能已暂停';
            return false;
        }
        $month_first_day = date('Y-m-01', strtotime(date("Y-m-d")));
        $month_last_day = date('Y-m-d', strtotime("$month_first_day +1 month -1 day"));

        //本月签到最新记录
        $record = $this->where('user_id', $user_id)
            ->where('last_sign_time', '>=', strtotime($month_first_day))
            ->where('last_sign_time', '<=', strtotime($month_last_day) - 1)
            ->order(['last_sign_time' => 'desc'])->find();

        if ($record) {
            $last_time = strtotime(date('Y-m-d', (int)$record['last_sign_time']));
            $curr_time = time();

            if ($last_time + 86400 > $curr_time) {
                $this->error = '今日已签到';
                return false;
            }

            $point = intval($setting['point']);
            //判断是否连续签到
            $cont = ($curr_time - $last_time) < 2 * 86400 ? true : false;
            //连续签到次数
            $times = $cont ? intval($record['lianxu_times']) + 1 : 0;

            if (!empty($setting['lianxu_rule']) && $setting['is_lianxu'] == 20) {
                $lianxu_rule = array_column($setting['lianxu_rule'], null, 'day');
                if (isset($lianxu_rule[$times])) {
                    $point += $lianxu_rule[$times]['num'];
                }
            }

            $this->point = $point;
            $time = $times;

        } else {
            $this->point = $setting['point'];
            $time = 0;
        }

        $this->startTrans();
        try {

            $user = \app\api\model\User::get($user_id);
            $point = $this->point;

            $t = time();
            $this->save([
                'user_id' => $user_id,
                'wxapp_id' => self::$wxapp_id,
                'last_sign_time' => $t,
                'points' => $point,
                'lianxu_times' => $time,
            ]);


            PointsLog::add([
                'user_id' => $user_id,
                'value' => $point,
                'describe' => "签到获得",
                'remark' => "签到获得",
            ]);

            (new \app\api\model\User())->where(['user_id'=>$user_id])->setInc('points', floatval($point));


            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }


    }


    public function dayList($start_time, $user_id, $num = 7)
    {

        $data = [];
        //获取第一天的,假设今天没签到
        $setting = Setting::getItem('sign');
        $month_first_day = date('Y-m-01', strtotime(date("Y-m-d")));
        $month_last_day = date('Y-m-d', strtotime("$month_first_day +1 month -1 day"));
        $lianxu_rule = array_column($setting['lianxu_rule'], null, 'day');
        $point = intval($setting['point']);
        $record = $this->where('user_id', $user_id)
            ->where('last_sign_time', '>=', strtotime($month_first_day))
            ->where('last_sign_time', '<=', strtotime($month_last_day) - 1)
            ->order(['last_sign_time' => 'desc'])->find();
        if ($record) {
            $last_time = strtotime(date('Y-m-d', (int)$record['last_sign_time']));
            $curr_time = $start_time;


            //判断是否连续签到
            $cont = ($curr_time - $last_time) < 2 * 86400 ? true : false;
            //连续签到次数
            $times = $cont ? intval($record['lianxu_times']) + 1 : 0;

            if (!empty($setting['lianxu_rule']) && $setting['is_lianxu'] == 20) {
                if (isset($lianxu_rule[$times])) {
                    $point += $lianxu_rule[$times]['num'];
                }
            }
        } else {

            $times = 0;
        }

        $is_today = $this->where('user_id', $user_id)->where('last_sign_time', '>=', strtotime(date('Y-m-d')))
            ->where('last_sign_time', '<=', strtotime(date('Y-m-d 23:59:59')))
            ->find();
        if ($is_today) {
            $data[] = [
                'day' => '今日',
                'point' => $is_today['points'],
            ];
            $times--;
        } else {
            $data[] = [
                'day' => '今日',
                'point' => $point,
            ];
        }


        //第二天到第n天
        for ($i = 0; $i < $num - 1; $i++) {


            $point = intval($setting['point']);


            if (date('m', $start_time + 86400 * ($i + 1)) != date('m', $start_time + 86400 * $i)) {
                $times = -1;
            } else {

            }


            $lianxu_times = ++$times;

            $res[] = [
                'times' => $times,
                'lianxu' => $lianxu_times,
                'data' => date('m-d', $start_time + 86400 * ($i + 1))
            ];

            if (!empty($setting['lianxu_rule']) && $setting['is_lianxu'] == 20) {

                if (isset($lianxu_rule[$lianxu_times])) {
                    $point += $lianxu_rule[$lianxu_times]['num'];
                }
            }
            $data[] = [
                'day' => date('m-d', $start_time + 86400 * ($i + 1)),
                'point' => $point
            ];
        }

        return $data;


    }
}
